<?php
/**
 * 尚善福· 登录注册模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2022 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com；
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Home\Controller;
use Think\Controller;
use Hoteladmin\Model\AdminModel;
class LoginController extends Controller {
    /**
     +--------------------------------------------------------------------------
     * 实现前台会员登录的功能
     * @author GH
     * @date 2018-12-25
     +--------------------------------------------------------------------------
     */
    public function login(){
        //实例化相Model
        $lidentr = new AdminModel();
        if(IS_POST){
            //接收传值
            $uname = trim(I('post.uname'));
            $upwds = md5(trim(I('post.pwds')));
            $ucode = trim(I('post.code'));
            //验证码
            if(check_verify($ucode)==false){
                $res = array('status'=>0,'msg'=>'验证码错误');
                exit(json_encode($res));
            }


            $res = $lidentr->user_login($uname,$upwds);
            if($res['status'] == 1){
                $res['url'] =  U("Index/index");
                $auth = array(
                    'usid'  => $res['result']['us_uid'],
                    'uname' => $res['result']['us_name'],
                    'phone' => $res['result']['us_phone'],
                    'state' => $res['result']['us_state'],
                    'utime' => $res['result']['us_ltime'],
                );
                session('user_auth', $auth);
                session('user_time', time());
                session('user_auth_sign', data_auth_sign($auth));
                //生成Cook
                $nickname = empty($res['result']['us_name']) ? $uname : $res['result']['us_name'];
                setcookie('uname',$nickname,null,'/');
                //首次登陆激活处理:激活账户，生成二位码
                $lidentr->first_login($res['result']['us_uid']);
                
                //登陆后记录操作日志
                addLog($res['result']['us_uid'],'1','1','登陆网站');
            }
            exit(json_encode($res));
        }else{
            //获取系统参数
            $reg = tpCache("sBas");            
            if(is_login('user_auth','user_auth_sign')){
                $this->redirect('Index/index');
            }else{
                $this->display();
            }
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现会员注册的功能
     * @author： GH
     * @date 2018-12-25
     +--------------------------------------------------------------------------
     */
    public function register(){
        //实例化相关表
        $smident = M('sms');
        $usident = M('user');
        //实例化相关Model
        $lidentr = new AdminModel();
        $reg = tpCache("sBas");
        $sms = tpCache("sMes");
        //dump($reg);die;
        if($reg["reg"]=='0'){
            $this->error($reg['regyuan'],'/');
        }
        if(IS_POST){
            //接收传值

            $upid  = trim(I('post.upid'));
            $phone = trim(I('post.phone'));
            $ucode = trim(I('post.code'));
            $scode = trim(I('post.scode'));
//            //验证短信验证码
//            if($sms['zhuce']=='1'){
//                $smsre = $smident->where("`sm_phone`='".$phone."'")->order("`sm_id` desc")->find();
//                //dump($smsre);
//                if(count($smsre)>0 && $scode!= $smsre['sm_code']){
//                    exit(json_encode(array('status'=>-1,'msg'=>'短信验证码不正确')));
//                }
//            }else{   //图像验证码
//                if(check_verify($ucode)==false){
//                    exit(json_encode(array('status'=>0,'msg'=>'验证码错误')));
//                }
//            }
            //验证推荐人信息
            $upFind = $usident->where("(`us_phone`='".$upid."') or (`us_uid`='".$upid."') and `us_state`=1")->getField('us_uid');
            //echo $usident->getLastSql();
            if(count($upFind)=='0'){
                exit(json_encode(array('status'=>-1,'msg'=>'推荐人信息不存在或未审核')));
            }
            //验证手机号
            $usFind = $usident->where("`us_phone`='".$phone."'")->getField('us_phone');
            if(count($usFind)!='0'){
                exit(json_encode(array('status'=>-1,'msg'=>'该手机号已被注册，请使用新手机号')));
            }
            //dump($sms['cheng']);die;
            $re = $lidentr->user_regist($upFind,$phone,$sms['cheng']);
            if('1'==$re){
                $res = array('status'=>1,'msg'=>'注册成功，需登录激活','url'=>U("Login/login"));
                exit(json_encode($res));
            }
        }else{
            $this->assign('upid',I('get.u'));
            $this->assign('sms',$sms['zhuce']);
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现前端发送短信方法
     * @author:XC跃
     * @date:2018-03-20
     +--------------------------------------------------------------------------
     */
    public function sendCode(){
        //接收值
        $scene = trim(I('post.scene'));    //发送短信验证码使用场景
        $phone = trim(I('post.phone'));
        $code  = trim(I('post.code'));
        //实例化相关表
        $smident = M('sms');
        $usident = M('user');
        //获取系统参数
        $config = tpCache("sMes");  
        
        $session_id = I('unique_id',session_id());
        session("scene" , $scene);
        //注册
        if('1'== $scene){
            //验证手机号
            $count = $usident->where("`us_phone`='".$phone."'")->count();
            if('0'!=$count){
                $res = array('status'=>-1,'msg'=>'该手机号已被注册，请使用新手机号');
                exit(json_encode($res));
            }if(check_verify($code)==false){
                $res = array('status'=>1,'msg'=>'图像验证码错误');
                exit(json_encode($res));
            }
        }
        //忘记密码
        if('2'==$scene){
            //验证手机号
            $count = $usident->where("`us_phone`='".$phone."'")->count();
            if('0'==$count){
                $res = array('status'=>-1,'msg'=>'该手机号未注册');
                exit(json_encode($res));
            }if(check_verify($code)==false){
                $res = array('status'=>1,'msg'=>'图像验证码错误');
                exit(json_encode($res));
            }
        }
        //检验能否发送短信
        $res = checkEnableSendSms($scene);
        if($res['status'] != 1){
            exit(json_encode($res));
        }
        //判断是否存在验证码
        $slFind = $smident->where("`sm_phone`='".$phone."' and `sm_session`='".$session_id."' and `sm_state`=1")->order('sm_id desc')->find();
        //120秒以内不可重复发送
        if($slFind && (time() - $slFind['sm_atime']) < 120){
            $res = array('status'=>-1,'msg'=>'120秒内不允许重复发送');
            exit(json_encode($res));
        }
        //随机一个验证码
        $code = rand(1000, 9999);        
        $params['code'] = $code;
        //发送短信
        $resp = sendSms($scene,$phone,$params,$session_id);
    
        if($resp['status'] == 1){
            //发送成功, 修改发送状态位成功
            $smmap['sm_phone']  = $phone;
            $smmap['sm_code']   = $code;
            $smmap['sm_session']= $session_id;
            $smmap['sm_state']  = '0';
            $smident->where($smmap)->save(array('sm_state'=>'1'));
            $res = array('status'=>0,'msg'=>'发送成功');
        }else{
            $res = array('status'=>-1,'msg'=>$resp['msg']);
        }
        exit(json_encode($res));
    }
    /**
     +--------------------------------------------------------------------------
     * 实现会员退出的功能
     * @author: XC跃
     * @date :2018-03-18
     +--------------------------------------------------------------------------
     */
    public function logout(){
        session('user_auth',null);
        session('user_time', null);
        session('user_auth_sign', null);
        setcookie('uname','',time()-3600,'/');
        header("location:".U('Login/login'));
        exit;
    }
    /**
     +--------------------------------------------------------------------------
     * 实现验证码的获取
     * @author: XC跃
     * @date :2018-03-01
     +--------------------------------------------------------------------------
     */
    public function vertify(){
        $config = array(
            'fontSize' => 35,
            'length'   => 4,
            'useCurve' => false,
            'useNoise' => false,
            'codeSet'  => '0123456789',
            'fontttf'  => '5.ttf',
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现openid的获取
     * @author: XC跃
     * @date :2018-03-01
    +--------------------------------------------------------------------------
     */
    public function openid(){
        $openid = getOpenid();
        echo $openid;
    }
}